package com.secsbrain.cmdb.dal.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;

@Configuration
public class ApplicationConfiguration {

    @Bean
    public Filter corsFilter(@Qualifier("corsconfig") CorsConfigurationSource configurationSource){
        return new CorsFilter(configurationSource);
    }

    @Bean(name="corsconfig")
    public CorsConfigurationSource corsConfigurationSource(){
        return new CorsConfigurationSource() {
            @Override
            public CorsConfiguration getCorsConfiguration(HttpServletRequest httpServletRequest) {
                CorsConfiguration configuration = new CorsConfiguration();
                configuration.addAllowedOrigin("*");
                configuration.addAllowedMethod("*");
                configuration.addAllowedHeader("*");

                return configuration;
            }
        };
    }
}
